package com.finance.system.service;

import com.finance.base.enums.StateEnum;
import com.finance.system.bean.pojo.Menu;
import com.finance.util.MenuUtil;
import com.alibaba.druid.util.StringUtils;
import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.system.bean.pojo.RoleMenuRel;
import com.finance.system.dao.MenuMapper;
import com.finance.system.dao.RoleMenuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CBWL on 2017/4/12.
 */
@Service
public class MenuService implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Menu> listAll() {
         return this.menuMapper.listAll();
    }

        @Override
        public Menu listMenuTree() {
            List<Menu> list = this.menuMapper.listAll();
            return MenuUtil.fillMenuTree(list);
        }

        @Override
        public List<Menu> listAllByRoleId(Long roleId) {
            return menuMapper.listCheckByRoleId(roleId);
        }

        @Override
        public Menu getById(Long id) {
            return menuMapper.getById(id);
        }

        @Override
        @Transactional
        public boolean add(Menu menu) throws BaseException {
            int count = menuMapper.countByCode(menu.getCode(),null);
            if(count >0){
                throw new BaseException(BaseConstants.ERR_DUPLICATE_DATA);
            }
            if(!StringUtils.isEmpty(menu.getPCode())){
                count = menuMapper.countByCode(menu.getPCode(),StateEnum.Valid.getState());
                if(count <= 0){
                    throw new BaseException(BaseConstants.ERR_NON_EXISTENT);
                }
            }
            return menuMapper.add(menu)==1;
        }

        @Override
        public boolean update(Menu menu) {
            return menuMapper.update(menu)==1;
        }

        @Override
        public boolean del(Long id, String uId) throws BaseException {
            int count = menuMapper.countLowerLevelById(id);
            if(count>0){
                throw new BaseException(BaseConstants.ERR_EXIST_LOWER);
            }
            return menuMapper.del(id,uId)==1;
        }

        @Override
        @Transactional
        public boolean updateRoleRel(Long roleId,Long uId,List<String> menuCodes) {
            List<RoleMenuRel> roleMenuRels = roleMenuMapper.listByRoleId(roleId);
            //需要插入的
            List<RoleMenuRel> newRels = new ArrayList<RoleMenuRel>();
            //需要更新失效的
            List<RoleMenuRel> invalidRels = new ArrayList<RoleMenuRel>();
            //需要更新有效的
            List<RoleMenuRel> validRels=new ArrayList<RoleMenuRel>();

            if(roleMenuRels != null && roleMenuRels.size()>0){
                for (RoleMenuRel rel : roleMenuRels){
                    if(menuCodes.contains(rel.getMenuCode())){
                        //包含
                        if(rel.getState()==0){
                            //包含的原来是失效的,跟新成有效的  原来有效的不管
                            RoleMenuRel roleMenuRel=new RoleMenuRel();
                            roleMenuRel.setState(StateEnum.Valid.getState());
                            roleMenuRel.setUpdateUid(uId);
                            roleMenuRel.setMenuCode(rel.getMenuCode());
                            roleMenuRel.setRoleId(roleId);
                            validRels.add(roleMenuRel);
                            //移除
                            menuCodes.remove(rel.getMenuCode());
                        }{
                            //移除
                            menuCodes.remove(rel.getMenuCode());
                        }
                    }else {
                        //不包含 使失效
                        //包含的原来是失效的,跟新成有效的  原来有效的不管
                        RoleMenuRel roleMenuRel = new RoleMenuRel();
                        roleMenuRel.setState(StateEnum.Invalid.getState());
                        roleMenuRel.setUpdateUid(uId);
                        roleMenuRel.setMenuCode(rel.getMenuCode());
                        roleMenuRel.setRoleId(roleId);
                        invalidRels.add(roleMenuRel);
                    }
                }

            }
            if(menuCodes != null && menuCodes.size()>0){
                for (String menuCode : menuCodes){
                    RoleMenuRel rel = new RoleMenuRel();
                    rel.setState(StateEnum.Valid.getState());
                    rel.setCreateUid(uId);
                    rel.setMenuCode(menuCode);
                    rel.setRoleId(roleId);
                    newRels.add(rel);
                }
            }
            if(newRels.size()>0){
                roleMenuMapper.batchAdd(newRels);
            }
            if(invalidRels.size()>0){
                roleMenuMapper.batchInvalid(uId,invalidRels);
            }
            if(validRels.size()>0){
                roleMenuMapper.batchValid(uId,validRels);
            }
            return true;
        }

        @Override
        public List<Menu> listAllMenusByUsercode(String usercode) {
            return menuMapper.listAllMenusByUsercode(usercode);
        }

        @Override
        public List<Menu> getByRoleId(Long roleId){
            return menuMapper.getByRoleId(roleId);
        }

    @Override
    public Menu getBySort(Integer sort, String pCode) {
        return menuMapper.getBySort( sort, pCode );
    }
}
